package encrypt;


import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


import java.util.Base64;

public class AesEncryptUtil {


    //使用AES-128-CBC加密模式，key需要为16位,key和iv可以相同！
    public static final String KEY = "1234567890114636";

    public static final String IV = "1234567890114636";

    //算法
    private static final String ALGORITHM = "AES";
    //默认的加密算法
    private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
    // 编码
    private static final String ENCODING = "UTF-8";
    // 偏移量
    private static final String OFFSET = "1234567890114636";


    /**
     * 加密方法
     *
     * @param data 要加密的数据
     * @param key  加密key
     * @param iv   加密iv
     * @return 加密的结果
     */
    public static String encrypt(String data, String key, String iv) {
        try {
            // 初始化cipher
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            // 转化成JAVA的密钥格式
            SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes("ASCII"), ALGORITHM);
            // 使用CBC模式，需要一个向量iv，可增加加密算法的强度
            IvParameterSpec vv = new IvParameterSpec(OFFSET.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, vv);
            byte[] encrypted = cipher.doFinal(data.getBytes(ENCODING));
            // 此处使用BASE64做转码。
            return Base64.getEncoder().encodeToString(encrypted);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 解密方法
     *
     * @param data 要解密的数据
     * @param key  解密key
     * @param iv   解密iv
     * @return 解密的结果
     */
    public static String desEncrypt(String data, String key, String iv) {
        try {
            byte[] encrypted1 = Base64.getDecoder().decode(data);

            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original);
            return originalString.trim();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 使用默认的key和iv加密
     */
    public static String encrypt(String data) throws Exception {
        return encrypt(data, KEY, IV);
    }

    /**
     * 使用默认的key和iv解密
     */
    public static String desEncrypt(String data) throws Exception {
        return desEncrypt(data, KEY, IV);
    }


    /**
     * 测试
     */
    public static void main(String args[]) throws Exception {

        String test = "123";
        String byteTest = new String(test.getBytes(), "UTF-8");
        String data = null;
        String key = KEY;
        String iv = IV;
        // /
        data = encrypt(byteTest, key, iv);
        System.out.println("数据：" + byteTest);
        System.out.println("加密：" + data);
        String jiemi = desEncrypt(data, key, iv).trim();
        System.out.println("解密：" + jiemi);


    }

}
